package com.suncaper.tddmall.controller;

//import com.suncaper.tddmall.common.model.User;
//import com.suncaper.tddmall.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.ModelMap;
//import org.springframework.web.bind.annotation.RequestMapping;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Date;

import com.suncaper.tddmall.common.model.Cart;
import com.suncaper.tddmall.common.model.Goods;
import com.suncaper.tddmall.common.model.User;
import com.suncaper.tddmall.service.CartService;
import com.suncaper.tddmall.service.GoodsService;
import com.suncaper.tddmall.service.UserService;
import com.suncaper.tddmall.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.security.util.Password;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LoginController {

    //    @Autowired
//    private UserService userService;
//    @RequestMapping("/login")
//    public String login(ModelMap modelMap, User user, HttpServletRequest request) {
//        User userDB = userService.findUserByUsername(user.getUsername());
//        boolean success = true;
//        String failMsg = null;
//        if (userDB == null) {
//            success = false;
//            failMsg = "用户名不存在";
//        } else if (!(userDB.getPassword().equals(user.getPassword()))) {
//            success = false;
//            failMsg = "密码不匹配";
//        }
//        modelMap.put("content", "main");
//        //modelMap.put("failMsg", failMsg);
//        if (success) {
//            request.getSession().setAttribute("current_user", userDB);
//            request.getSession().setAttribute("loginTime", new Date());
//
//        }
//        return  "login";
//
//    }
    @Resource
    @Autowired
    UserService userService;
    @Autowired
    CartService cartService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping("/login")
    public String Login(ModelMap modelMap, String username, String password, HttpServletRequest request, Model model) {
        boolean success = true;
        String failMsg = null;
        String a = "index";
        if (username == null || password == null) {
            success = false;
            failMsg = "用户名不能为空";
            a = "index";
        }
        User user = userService.login(username, password);
//        System.out.println("12314546");

        if (user == null) {
            success = false;
            failMsg = "用户名或密码不正确";
//            a="login";
        } else {
            request.setAttribute("user", user);
            request.getSession().setAttribute("user", user);
            List<Goods> goodsList = goodsService.selectAll();
//            System.out.println("登录成功");
            int i = 0;
            List<Cart> carts = cartService.selectAll(user.getUserId());
            for (Cart cart : carts) {
                Goods goods = goodsService.selectByPrimaryKey(cart.getGoodsId());
                cart.setGoods(goods);
                i += 1;
            }
            String s = i+"";
            model.addAttribute("cartList", carts);
            model.addAttribute("count", s);
            model.addAttribute("goodsList",goodsList);

            success = false;
            failMsg = "密码不匹配";
            return "index";
        }

        modelMap.put("content", "main");
        modelMap.put("failMsg", failMsg);
        return a;
    }

    @RequestMapping("/toLogout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "index";
    }


    @RequestMapping("/toregister")
    public String toregister() {
        return "register";
    }


//        String username = user.getUsername();
//        if (userService.findByUsername(username) == null) {
//            // 添加用户
//            userService.register(user);
//            System.out.println("1234");
//
//        return "register";}else{
////            model.addAttribute("msg", "注册失败");
//            return "register";
//        }


    @RequestMapping("/register")
    public String register(@RequestParam("email") String email, @RequestParam("password") String password, @RequestParam("username") String username) {


//        String b= "index" ;
        if (userService.findByUsername(username) == null) {
            User user1 = new User();
            user1.setUsername(username);
            user1.setPassword(password);
            user1.setEmail(email);
            user1.setUserId(username);
            userService.register(user1);
            System.out.println("注册成功");
            return "index";
        } else {
            System.out.println("注册不成功但运行");
            return "register";
        }
    }


    @RequestMapping("/home")
    public String ChangePassword() {

        return "home";
    }

    @RequestMapping("/pages-profile")
    public String PagesProfiles() {

        return "pages-profile";
    }

    @RequestMapping("/pages-basic")
    public String PagesBasic() {

        return "pages-basic";
    }

}

